package com.itheima.info.manager.controller;

import com.itheima.info.manager.domain.Student;
import com.itheima.info.manager.service.StudentService;

import java.util.Scanner;

public class StudentController {
    Scanner sc = new Scanner(System.in);
    StudentService studentService = new StudentService();
    public void StudentStart() {

        while (true) {
            System.out.println("--------欢迎来到 <学生> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加学生  2.删除学生  3.修改学生  4.查看学生  5.退出");
            String choice = sc.nextLine();
            switch (choice) {
                case "1":
                    //添加学生
                    addStudent();
                    break;
                case "2":
                    //删除
                    deleteStudent();
                    break;
                case "3":
                    //修改
                    updataStudent();
                    break;
                case "4":
                    //查看
                    findStudent();
                    break;
                case "5":
                    //退出
                    return;
                default:
                    System.out.println("欢迎使用学生管理系统,再见!");
                    break;
            }
        }
    }

    public void updataStudent() {
        System.out.println("请输入您要修改的学号:");
        String updataId;
        while (true) {
            updataId = sc.nextLine();
            boolean flag = studentService.isExists(updataId);
            if (flag){
                break;
            }else {
                System.out.println("您输入的学号不存在,请重新输入:");
            }
        }
        System.out.println("请输入修改的学生姓名:");
        String name = sc.nextLine();
        System.out.println("请输入修改的学生年龄:");
        String age = sc.nextLine();
        System.out.println("请输入修改的学生生日:");
        String birthday = sc.nextLine();

        Student stu = new Student(updataId,name,age,birthday);

       studentService.updataStudent(updataId,stu);
        System.out.println("修改成功!");
    }

    public void deleteStudent() {
        System.out.println("请输入您要删除的学号:");
        String delId;
        while (true) {
            delId = sc.nextLine();
            boolean flag = studentService.isExists(delId);
            if (flag){
                break;
            }else {
                System.out.println("您输入的学号不存在,请重新输入:");
            }
        }
        studentService.deleteStudent(delId);
        System.out.println("删除成功!");
    }

    public void findStudent() {
        Student[] students = studentService.findStudent();
        if(students!=null) {
            System.out.println("学号\t\t姓名\t年龄\t生日");
            for (int i = 0; i < students.length; i++) {
                Student stu = students[i];
                if (stu != null) {
                    System.out.println(stu.getId() + "\t" + stu.getName() + "\t" + stu.getAge() + "\t" + stu.getBirthday());
                }
            }
        }else{
            System.out.println("查无信息,请添加!");
        }

    }

    public void addStudent() {
        //判断是否存在ID;
        String id;
        System.out.println("请输入添加的学生学号:");
        while (true) {
            id = sc.nextLine();
            boolean result = studentService.isExists(id);
            if (!result){
                break;
            }
            System.out.println("您输入的学号已存在,请重新输入:");
        }

        System.out.println("请输入添加的学生姓名:");
        String name = sc.nextLine();
        System.out.println("请输入添加的学生年龄:");
        String age = sc.nextLine();
        System.out.println("请输入添加的学生生日:");
        String birthday = sc.nextLine();

        Student stu = new Student(id,name,age,birthday);

        boolean result =studentService.addStudent(stu);
        if (result){
            System.out.println("添加成功!");
        }else{
            System.out.println("添加失败,空间已满");
        }

    }
}
